﻿#define ChatGroup

using MyBot.Api;
using MyBot.Datas;
using MyBot.Expansions.Bot;
using MyBot.Models.MessageModels;
using Newtonsoft.Json.Linq;
using QQRobot.AMiao.Help;
using QQRobot.AMiao.Instructions;
using QQRobot.Services;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using static QQRobot.Services.Models.Response.BaiduTranslateResponse;

namespace QQRobot.AMiao
{
    /// <summary>
    /// 逆天而行
    /// </summary>
    public class DaYuanShuaiRobot
    {

        // 声明鉴权信息
        private static OpenApiAccessInfo OpenApiAccess()
        {
            OpenApiAccessInfo openApiAccessInfo = new OpenApiAccessInfo()
            {
                BotQQ = "3889369395",
                BotAppId = "102456579",
                BotToken = "CXXXF7gIISkgOLyudK6VQNgznhwI8G5q",
                BotSecret = "kYMAymbQF4tiYOE4ukbSJA1sjbTLD5xq",
            };
            return openApiAccessInfo;
        }



        public static async Task OnLineRobot()
        {
            // 鉴权信息在实例化时传入
            var qChannelApi = new QQChannelApi(OpenApiAccess());

            // 指定Api请求使用Bot身份
            qChannelApi.UseBotIdentity();

            // 指定Api通道模式为沙盒模式 (测试时使用)
            // 不指定的情况下默认是正式模式
            qChannelApi.UseSandBoxMode();
            // 实例化一个 ChannelBot,该类是一个容易理解且简单的类
            // 帮助你快速实现一个利于理解学习与开发的机器人原型
            // 将鉴权信息 (openApiAccessInfo) 传入构造函数
            ChannelBot channelBot = new(qChannelApi);
            //指定为私域机器人
            channelBot.UsePrivateBot();
            // 注册群聊消息事件
            channelBot.RegisterChatEvent();

            channelBot.RegisterUserMessageEvent();
            // 为链接官方平台成功事件绑定一个回调函数
            channelBot.RegisterInteractionEvent();


            channelBot.OnConnected += () =>
            {
                System.Console.WriteLine("链接成功!");
            };
            // 为鉴权成功事件绑定一个回调函数
            channelBot.AuthenticationSuccess += () =>
            {
                System.Console.WriteLine("机器人已上线");
            };
            // 为机器人出现异常事件绑定一个回调函数
            channelBot.OnError += (ex) =>
            {
                System.Console.WriteLine($"机器人出现错误 -> {ex.Message}");
            };
        


           channelBot.ReceivedChatGroupMessage += async (message) =>
            {
                //await Command.GroupCommandImplement(message, qChannelApi);
                var msgId = message.Id; //消息ID
                var content = message.Content;//消息内容
                _ = message.Author.MemberOpenId; //群聊消息发送者的OpenId
                var groupId = message.GroupOpenId; //群聊消息发送者的OpenId
                var msg = qChannelApi.GetChatMessageApi();
                var attachments = message.Attachments;
                var key1 = "/AI对话绘图";
                if (content.Contains(key1))
                {
                    content = StrHelp.RemoveStr(content, key1);
                    var translates = await BaiduAPI.GetBaiduTranslateAsync(content);
                    var tran = translates.trans_result.FirstOrDefault();
                    if (tran == null)
                    {
                        await msg.SendGroupMessageAsync(
                                       groupId,
                                         $"不好意思，您的机器人思路有点乱，请稍后重试",
                                       msgType: ChatMessageType.Text,

                                       //markdown: new MessageMarkdown { Content = "点击这里访问 [链接](https://www.example.com)" },
                                       passiveMsgId: msgId
                                       );
                    }
                    else
                    {
                        content = tran.dst;
                    }
                    var img = await SiliconFlowAPI.GetImgGenerationsAsync(new Services.Models.Request.ImgGenerationsRequest
                    {
                        prompt = content
                    });
                    if (img != null)
                    {
                        var media = await msg.SendGroupMediaAsync(
                           groupId,
                           mediaType: ChatMediaType.Image,
                           img?.images?.FirstOrDefault().url,
                           false
                           );
                        await msg.SendGroupMessageAsync(
                                  groupId,
                                    $"生成成功，请查收",
                                  msgType: ChatMessageType.Media,
                                  media: new ChatMessageMedia { FileInfo = media.FileInfo },
                                  //markdown: new MessageMarkdown { Content = "点击这里访问 [链接](https://www.example.com)" },
                                  passiveMsgId: msgId
                                  );
                    }
                    else
                    {
                        await msg.SendGroupMessageAsync(
                                     groupId,
                                       $"不好意思，您的机器人思路有点乱，请稍后重试",
                                     msgType: ChatMessageType.Text,

                                     //markdown: new MessageMarkdown { Content = "点击这里访问 [链接](https://www.example.com)" },
                                     passiveMsgId: msgId
                                     );
                    }
                }
                else
                {
                    var media = await msg.SendGroupMediaAsync(
                       groupId,
                       mediaType: ChatMediaType.Image,
                       "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png",
                       false
                       );
                    //await msg.SendGroupMessageAsync(
                    //    groupId,
                    //      $"收到消息啦！您发送的消息为 ->{message.Content}",
                    //    msgType: ChatMessageType.Media,
                    //    media: new ChatMessageMedia { FileInfo = media.FileInfo },
                    //    //markdown: new MessageMarkdown { Content = "点击这里访问 [链接](https://www.example.com)" },
                    //    passiveMsgId: msgId
                    //    );
                    #region markdown 目前还没有开通

                    //await msg.SendGroupMessageAsync(
                    //    openId: groupId,
                    //    content: $"收到消息啦！您发送的消息为 ->{message.Content},开始生成Markdown",
                    //    msgType: ChatMessageType.Markdown,
                    //    //media: new ChatMessageMedia { FileInfo = media.FileInfo },
                    //    markdown: new MessageMarkdown
                    //    {
                    //        //TemplateId = "102456579_1732006494",
                    //        CustomTemplateId = "102456579_1732008538",
                    //        Params = new List<MessageMarkdownParams>()
                    //        {
                    //            new MessageMarkdownParams("title",message.Content ),
                    //            new MessageMarkdownParams("image","https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" ),
                    //            new MessageMarkdownParams ("para1","段落1"),
                    //            new MessageMarkdownParams ("para2","段落2"),
                    //            new MessageMarkdownParams ("desc","简介"),
                    //            new MessageMarkdownParams ("content","在这非常开心"),
                    //            new MessageMarkdownParams ("link_introduction","链接介绍"),
                    //            new MessageMarkdownParams ("link","https://www.qq.com")
                    //        }
                    //    },
                    //    passiveMsgId: msgId
                    //    );


                    //await msg.SendGroupMessageAsync(
                    //  openId: groupId,
                    //  content: $"收到消息啦！您发送的消息为 ->{message.Content},开始生成Markdown",
                    //  msgType: ChatMessageType.Markdown,
                    //  //media: new ChatMessageMedia { FileInfo = media.FileInfo },
                    //  markdown: new MessageMarkdown
                    //  {
                    //      //TemplateId = "102456579_1732006494",
                    //      CustomTemplateId = "102456579_1732071578",
                    //      Params = new List<MessageMarkdownParams>()
                    //      {
                    //         new MessageMarkdownParams ("date",DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")),
                    //         new MessageMarkdownParams ("link_introduction","百雀羚化妆品涉嫌添加禁用原料属实"),
                    //         new MessageMarkdownParams ("link","https://www.baidu.com/"),
                    //         new MessageMarkdownParams ("link_introduction2","百雀羚化妆品涉嫌添加禁用原料属实"),
                    //         new MessageMarkdownParams ("link2","https://www.baidu.com/"),
                    //         new MessageMarkdownParams ("link_introduction3","百雀羚化妆品涉嫌添加禁用原料属实"),
                    //         new MessageMarkdownParams ("link3","https://www.baidu.com/"),
                    //         new MessageMarkdownParams ("link_introduction4","百雀羚化妆品涉嫌添加禁用原料属实"),
                    //         new MessageMarkdownParams ("link4","https://www.baidu.com/"),
                    //      }
                    //      //Params = new List<MessageMarkdownParams>()
                    //      //{
                    //      //      new MessageMarkdownParams("title",message.Content ),
                    //      //      new MessageMarkdownParams("image","https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" ),
                    //      //      new MessageMarkdownParams ("para1","段落1"),
                    //      //      new MessageMarkdownParams ("para2","段落2"),
                    //      //      new MessageMarkdownParams ("desc","简介"),
                    //      //      new MessageMarkdownParams ("content","在这非常开心"),
                    //      //      new MessageMarkdownParams ("link_introduction","链接介绍"),
                    //      //      new MessageMarkdownParams ("link","https://www.qq.com")
                    //      //}
                    //  },
                    //  passiveMsgId: msgId
                    //  );

                    await msg.SendGroupMessageAsync(
                         openId: groupId,
                         msgType: ChatMessageType.Markdown,
                         media: new ChatMessageMedia { FileInfo = media.FileInfo },
                         //markdown: new MessageMarkdown
                         //{
                         //    CustomTemplateId = "102456579_1732071578",
                         //    Params = new List<MessageMarkdownParams>()
                         //    {
                         //      new MessageMarkdownParams ("date",DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")),
                         //      new MessageMarkdownParams ("link_introduction","百雀羚化妆品涉嫌添加禁用原料属实"),
                         //      new MessageMarkdownParams ("link","https://www.baidu.com/"),
                         //      new MessageMarkdownParams ("link_introduction2","百雀羚化妆品涉嫌添加禁用原料属实"),
                         //      new MessageMarkdownParams ("link2","https://www.baidu.com/"),
                         //      new MessageMarkdownParams ("link_introduction3","百雀羚化妆品涉嫌添加禁用原料属实"),
                         //      new MessageMarkdownParams ("link3","https://www.baidu.com/"),
                         //      new MessageMarkdownParams ("link_introduction4","百雀羚化妆品涉嫌添加禁用原料属实"),
                         //      new MessageMarkdownParams ("link4","https://www.baidu.com/")
                         //    },
                         //    Content ="测试消息"
                         //},
                         keyboard: new MessageKeyboard { Id = "102456579_1732085333" },
                         passiveMsgId: msgId
                     );
                    #endregion
                }
            };

            channelBot.ReceivedChatUserMessage += async (message) =>
            {
                var msgId = message.Id;
                var content = message.Content;
                var openId = message.Author.UserOpenId;
                var msg = qChannelApi.GetChatMessageApi();

                var media = await msg.SendUserMediaAsync(
                     openId,
                     mediaType: ChatMediaType.Image,
                     "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png",
                     false
                     );

                await msg.SendUserMessageAsync(
                    openId,
                      $"收到消息啦！您发送的消息为 ->{message.Content}",
                    msgType: ChatMessageType.Media,
                    media: new ChatMessageMedia { FileInfo = media.FileInfo },
                    passiveMsgId: msgId
                    );
            };

            channelBot.ReceivedUserMessage += async (message) =>
            {
                var msgId = message.Id;
                var content = message.Content;
                var openId = message.Author.Id;
                var msg = qChannelApi.GetChatMessageApi();

                await msg.SendUserMessageAsync(
                    openId,
                    $"收到消息啦！您发送的消息为 ->{message.Content}",
                    msgType: ChatMessageType.Text,

                    passiveMsgId: msgId
                    );
            };

            channelBot.SubscribeMessageReceive += async (message) =>
                {
                    var openId = message.OpenId;
                    var groupOpenId = message.GroupOpenId;
                    var content = message.Result;
                    var msgAPI = qChannelApi.GetChatMessageApi();
                    await msgAPI.SendGroupMessageAsync(
                        groupOpenId,
                          $"订阅消息",
                        msgType: ChatMessageType.Markdown
                        //markdown: new MessageMarkdown { Content = "点击这里访问 [链接](https://www.example.com)" },
                        );
                };

            // 为接收到@机器人事件绑定一个回调函数
            // message 为收到的消息内容
            channelBot.ReceivedAtMessage += async (message) =>
            {
                // 使用QQChannelApi异步回复一个与用户发送一样的消息内容

                // 发送消息时，如果机器人中的一些能力未联系官方开通，会导致发送失败，或者发送成功但需要等待官方人工审核
                // 发送失败或需要等待官方审核时会抛出异常，请注意捕获
                await qChannelApi
                    .GetMessageApi()
                    .SendTextMessageAsync(message.ChannelId, $"@消息！您发送的消息为 -> {message.Content}", message.Id);
            };

            channelBot.ReceivedDirectMessage += async (message) =>
            {
                await qChannelApi
                       .GetMessageApi()
                       .SendTextMessageAsync(message.ChannelId, $"私聊消息！您发送的消息为 -> {message.Content}", message.Id);
            };


            // 完成以上配置后将机器人上线
            await channelBot.OnlineAsync();

            // 因为是异步处理，所以需要阻塞控制台 
            // 否则控制台会闪退(因为此时同步代码已经执行完毕)
            await Task.Delay(-1);

            // 因为是异步处理，所以需要阻塞控制台 
            // 否则控制台会闪退(因为此时同步代码已经执行完毕)
            // 输入q退出控制台程序
            while (Console.ReadLine().ToUpper() != "Q")
            {

            }
            System.Console.WriteLine("程序已退出..");

        }




    }
}
